package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.system.domain.business.Model;
import com.lvy.hczn.front.business.mapper.ModelMapper;
import com.lvy.hczn.front.business.service.ModelService;
import com.lvy.hczn.front.common.exception.UtilException;
import io.socket.client.IO;
import io.socket.client.Socket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;

@Slf4j
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements ModelService {

    @Autowired
    private ModelMapper modelMapper;


    @Override
    public void insert(Model model) throws Exception {
        if(model == null){
            throw new UtilException("");
        }
        model.setCreateDate(LocalDate.now());
        model.setDateInfo(new Date());
//        model.setDel(0);
        model.setName(RandomUtil.randomString(30));
        model.setPrice(RandomUtil.randomBigDecimal(new BigDecimal(10000),new BigDecimal(99999)));
        modelMapper.insert(model);
    }

    public static void main(String[] args) {
        // 服务端socket.io连接通信地址
        String url = "http://192.168.2.109:9092";
        try {
            IO.Options options = new IO.Options();
            options.transports = new String[]{"websocket"};
            options.reconnectionAttempts = 5;
            // 失败重连的时间间隔
            options.reconnectionDelay = 1000;
            // 连接超时时间(ms)
            options.timeout = 500;
            // userId: 唯一标识 传给服务端存储
            final Socket socket = IO.socket(url + "?userId=1", options);

//            socket.on(Socket.EVENT_CONNECT, args1 -> socket.send("hello..."));
            socket.on(Socket.EVENT_CONNECT, args1 -> System.out.println("打开连接。。。1"));
/*            socket.on(Socket.EVENT_CONNECT, args12 -> {
                log.info("打开连接。。。");
                System.out.println("打开连接。。。1");
                socket.send("hello");
            });*/
            socket.on(Socket.EVENT_DISCONNECT, args1 -> System.out.println("连接关闭"));

            // 自定义事件`connected` -> 接收服务端成功连接消息
            socket.on("message", objects -> System.out.println("服务端:" + objects[0].toString()));

            // 自定义事件`push_data_event` -> 接收服务端消息
            socket.on("push_data_event", objects -> System.out.println("服务端:" + objects[0].toString()));

            // 自定义事件`myBroadcast` -> 接收服务端广播消息
            socket.on("myBroadcast", objects -> System.out.println("服务端：" + objects[0].toString()));

            socket.connect();

            while (true) {
                Thread.sleep(3000);
                // 自定义事件`push_data_event` -> 向服务端发送消息
                log.info("发送消息测试");
                socket.emit("push_data_event", "发送数据 ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
